package com.example.pingze.controller;

import com.example.pingze.entity.Comment;
import com.example.pingze.entity.CommentUserLike;
import com.example.pingze.entity.Creation;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.CommentService;
import com.example.pingze.service.CreationService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/comments")
@CrossOrigin(origins = "*")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private CreationService creationService;

    //添加评论
    @PostMapping("/addComment")
    public Result<String> addComment(@RequestBody Comment comment) {
        int num = 0;
        if (comment.getCreationId() == null) {
            throw new PingZeException("comments_创作ID为空");
        }
        if (comment.getNickName() == null) {
            throw new PingZeException("comments_昵称为空");
        }
        if (comment.getContent() == null) {
            throw new PingZeException("comments_内容为空");
        }
        if (comment.getTime() == null) {
            throw new PingZeException("comments_时间为空");
        }
        if (comment.getAccount() == null) {
            throw new PingZeException("comments_账号为空");
        }
        comment.setId(UUID.randomUUID().toString());
        comment.setLikes(0);
        Comment comment1 = commentService.addComment(comment);

        //更新creation表中的comment字段
        Creation creation = creationService.findCreationById(comment.getCreationId());
        int i = creation.getComment();
        if (i >= 0) {
            num = i + 1;
            creation.setComment(num);
            creationService.saveCount(creation);
        }
        System.out.println(comment1);
        return ResultVOUtils.result(num);
    }

    //通过创作id获取该条创作的评论列表
    @PostMapping("/getComments")
    public Result<List<Comment>> getComments(@RequestParam String creationId) {
        if (creationId == null) {
            throw new PingZeException("comment_创作ID为空");
        }
        List<Comment> comments = commentService.findCommentsByCreationId(creationId);
        if (comments == null || comments.size() == 0) {
            throw new PingZeException("该条创作还没有评论");
        }else {
            return ResultVOUtils.result(comments);
        }
    }
    //通过评论id获取每条评论的点赞数
    @PostMapping("/getLikes")
    public Result getLikes(String commentId) {
        if (commentId == null) {
            throw new PingZeException("comment_评论ID为空");
        }
        Comment comment = commentService.findCommentById(commentId);
        if (comment == null) {
            throw new PingZeException("comment_该条评论不存在");
        }else {
            Integer likes = comment.getLikes();
            System.out.println(likes);
            return ResultVOUtils.result(likes);
        }
    }
    //删除评论
    @PostMapping("/deleteComment")
    public Result deleteComment(@RequestParam String id,
                                @RequestParam String account){
        int num = 0;
        Result result = new Result();
        if (id == null) {
            throw new PingZeException("id为空");
        }
        if (account == null) {
            throw new PingZeException("account为空");
        }
        Comment comment = commentService.findCommentById(id);
        if (comment == null) {
            throw new PingZeException("评论为null");
        }else {
            String creationId = comment.getCreationId();
            Creation creation = creationService.findCreationById(creationId);
            int i = creation.getComment();
            if (i > 0) {
                num = i - 1;
                creation.setComment(num);
                creationService.saveCount(creation);
                if (account.equals(comment.getAccount())) {
                    commentService.deleteCreation(comment);
                    result.setCode("150");
                    result.setMsg("该条评论删除成功");
                    result.setData(num);
                    return result;
                }
            }
        }
        return ResultVOUtils.success("评论删除成功");
    }
//    //通过creationId获取该条创作的评论数
//    @PostMapping("/getCounts")
//    public Result getCounts(@RequestParam String creationId) {
//        if (creationId == null) {
//            throw new PingZeException("comment_未获取到creationId");
//        }
//        List<Comment> comments = commentService.findCommentsByCreationId(creationId);
//        int count = comments.size();
//        System.out.println(count);
//        return ResultVOUtils.result(count);
//    }
}
